#include "_Music.h"
#ifdef _ANDROID_PLATFORM__
#include "SDL_log.h"
#else
#include <SDL2/SDL_log.h>
#endif


void _Music::loadMusic(std::string musicId, std::string fileName) {
  //如果不存在
  if (music_list.find(musicId) == music_list.end()) {
    Mix_Music* music = Mix_LoadMUS(fileName.c_str());
    if (music != nullptr) {
      music_list.insert(std::pair<std::string, Mix_Music*>(musicId, music));
    } else {
      SDL_LogError(0, "音乐文件加载失败,id:%s,%s\n", musicId.c_str(),
                   SDL_GetError());
    }
  } else {
    SDL_LogError(0, "此音乐已加载或id已被占用:id:%s\n", musicId.c_str());
  }
}

void _Music::playMusic(std::string musicId, bool isLoop) {
    if(music_list.find(musicId)!=music_list.end()){
        Mix_PlayMusic(music_list[musicId],(isLoop)?1:0);

    }else{
        SDL_LogError(0,"音乐无法播放，该id不存在：%s",musicId.c_str());
    }
}


_Music::~_Music(){
    for(auto i : music_list){
        Mix_FreeMusic(i.second);
    }
}
